<?php
// +----------------------------------------------------------------------
// | sfadmin - Suifeng Administration
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2020 http://suifeng.tech All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: suifeng <1787585212@qq.com>
// +----------------------------------------------------------------------

namespace suifeng\builder\table\button;

use suifeng\helper\HtmlHelper;
use suifeng\helper\ClassHelper;
use suifeng\helper\BaseObject;

class Button extends BaseObject
{
    /**
     * Table对象。
     *
     * @var unknown
     */
    public $table;

    /**
     * URL。
     *
     * @var unknown
     */
    public $url;

    /**
     * ICON。
     *
     * @var unknown
     */
    public $icon;

    /**
     * 标题。
     *
     * @var unknown
     */
    public $title;

    /**
     * AJAX处理方式，GET或POST。
     * @var unknown
     */
    public $ajax_method;

    /**
     * 是否需要确认？
     *
     * @var string
     */
    public $need_confirm = false;

    /**
     * 属性数组。
     *
     * @var array
     */
    public $attributes = array();

    /**
     * 输出方法。
     *
     * @var [type]
     */
    public $callback;

    /**
     * 构造方法。
     *
     * @param string $config
     */
    public function __construct($config = array())
    {
        parent::__construct($config);

        if ($this->title === null) {
            $this->title = strtoupper(ClassHelper::short_name($this));
        }

        if (empty($this->url)) {
            $this->url = url(request()->module() . '/' . request()->controller() . '/' . strtolower(ClassHelper::short_name($this)));
        }

        if (empty($this->attributes['class'])) {
            $this->attributes['class'] = 'btn btn-default-outline btn-pill';
        }
    }

    /**
     * 输出HTML。
     *
     * @param unknown $row
     * @return unknown|string
     */
    public function render_html()
    {
        // 如果有自定义的输出方法，则调用自定义方法输出。
        if ($this->callback) {
            return ClassHelper::invoke_callback($this->callback, [$row]);
        }

        $attributes = $this->attributes;

        if (empty($attributes['title'])) {
            $attributes['title'] = $this->title;
        }

        HtmlHelper::add_class($attributes, 'button-' . strtolower(ClassHelper::short_name($this)));

        if ($this->ajax_method) {
            HtmlHelper::add_class($attributes, 'ajax-' . strtolower($this->ajax_method));
        }

        if ($this->need_confirm) {
            HtmlHelper::add_class($attributes, 'confirm');
        }

        if ($this->url) {
            $attributes['href'] = $this->url;
        } else {
            $attributes['href'] = '#';
        }

        return HtmlHelper::render_tag('a', $attributes,
            array(
                $this->icon ? HtmlHelper::render_icon($this->icon) : '',
                $this->title ? '&nbsp;' . $this->title . '&nbsp;' : '',
            ));
    }

    /**
     * 工厂方法。
     *
     * @param unknown $type
     * @param string $attributes
     * @throws \Exception
     * @return mixed
     */
    public static function create($type, $config = array())
    {
        if (is_array($type)) {
            $config = $type;

            if (isset($config['type'])) {
                $type = $config['type'];
                unset($config['type']);
            } else if (isset($config['button'])) {
                $type = $config['button'];
                unset($config['button']);
            } else {
                $type = 'button';
            }
        }

        $class_name = '\\suifeng\\html\\table\\buttons\\' . implode('', array_map('ucfirst', explode('_', $type)));
        if (!class_exists($class_name)) {
            throw new \Exception('Button type not exists: ' . $type);
        }

        return new $class_name($config);
    }
}
